package com.tiancheng.onecode.controller.admin;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.tiancheng.onecode.exception.Asserts;
import com.tiancheng.onecode.model.dto.BucketPolicyConfigDto;
import com.tiancheng.onecode.model.dto.MinioUploadDto;
import com.tiancheng.onecode.model.response.Result;
import com.tiancheng.onecode.service.IMerchantPayCertService;
import com.tiancheng.onecode.service.IMerchantService;
import com.tiancheng.onecode.service.IUploadService;
import com.tiancheng.onecode.trade.TradeApiClient;
import com.tiancheng.onecode.trade.enums.IdPhotoEnums;
import com.tiancheng.onecode.trade.model.response.MerchantPayRegFileResVO;
import io.minio.*;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;

import static com.alibaba.nacos.api.PropertyKeyConst.ENDPOINT;

/**
 * @Author: kellen
 * @Description: 图片上传
 * @Date: create in 2024/12/13 09:25
 */
@Slf4j
@Controller
@RequestMapping("/admin/upload")
public class UploadController {

    @Resource
    private IUploadService uploadService;

    @Resource
    private TradeApiClient tradeApiClient;
    @Resource
    private IMerchantPayCertService merchantPayCertService;

    @RequestMapping(value = "/minio", method = RequestMethod.POST)
    @ResponseBody
    public Result<MinioUploadDto> upload(@RequestPart("file") MultipartFile file) {
        final MinioUploadDto minioUploadDto = uploadService.minioUpload(file);
        if(null!=minioUploadDto){
            return Result.success(minioUploadDto);
        }
        return Result.fail("上传失败！",null);
    }


    @RequestMapping(value = "/minio/delete", method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@RequestParam("objectName") String objectName) {
        final Boolean aBoolean = uploadService.deleteMinioObj(objectName);
        if(null!=aBoolean&& aBoolean){
            return Result.success();
        }
        return Result.fail("删除失败！");
    }



    @RequestMapping(value = "/trade", method = RequestMethod.POST)
    @ResponseBody
    public Result<MinioUploadDto> upload2Trade(@RequestPart("file") MultipartFile file) {
        final MerchantPayRegFileResVO upload = tradeApiClient.upload(file,null);
        if(null!=upload){
            final MinioUploadDto uploadDto = new MinioUploadDto();
            uploadDto.setUrl(upload.getInnerFilePath());
            uploadDto.setName(upload.getFileName());
            return Result.success(uploadDto);
        }
        return Result.fail("上传失败！",null);
    }

    @RequestMapping(value = "/trade/v2", method = RequestMethod.POST)
    @ResponseBody
    public Result<MinioUploadDto> upload2TradeNew(@RequestPart("file") MultipartFile file,@RequestParam("documentType") String documentType) {
        Optional<IdPhotoEnums> idPhotoEnums = IdPhotoEnums.getByCode(documentType);
        Asserts.isTrue(StringUtils.isNotEmpty(documentType)&& idPhotoEnums.isPresent(),"不支持的文件分类！");
        final MerchantPayRegFileResVO upload = tradeApiClient.upload(file,documentType);
        if(null!=upload){
            final MinioUploadDto uploadDto = new MinioUploadDto();
            uploadDto.setFileId(upload.getFileId());
            uploadDto.setUrl(upload.getInnerFilePath());
            uploadDto.setName(idPhotoEnums.get().getName());
            return Result.success(uploadDto);
        }
        return Result.fail("上传失败！",null);
    }

    @DeleteMapping(value = "/trade/delete")
    @ResponseBody
    public Result deleteFile(@RequestParam("fileId") String fileId) {
        merchantPayCertService.deleteByFileId(fileId);
        tradeApiClient.deleteFile(fileId);
        return Result.success();
    }
}
